import requests
# 定义一个类
class CinfigHttp:

    def __init__(self, dic):
        # 2.1.1 获取第一条测试用例内接口请求需要的关键字段:url,method,value,expect
        self.url = dic["interfaceUrl"]
        self.method = dic["method"]
        self.value = dic["value"]
        self.expect = dic["expect"]
        self.header = dic["header"]

    def run(self):
        # 2.1.2 判断: method的值
        # 如果method == get 进行get请求
        if self.method.lower() == "get":
            res = requests.get(url=self.url,
                               params=eval(self.value),
                               headers=eval(self.header))
        # 如果method == post 进行post请求
        elif self.method.lower() == "post":
            res = requests.post(url=self.url,
                                data=eval(self.value),
                                headers=eval(self.header))
        return res


if __name__ == '__main__':
    # 准备测试数据
    from common.readData import ReadData

    rd = ReadData()
    data = rd.read_excel()
    # 实例化
    ch = CinfigHttp(data[0])
    res = ch.run()
    print(res.text)
    print(res.headers)
